package com.longcai.service.impl.nearbyshop;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.nearby.ClyjNearbyShops;
import com.longcai.entity.nearby.ClyjNearbyShopsScheme;
import com.longcai.mapper.ClyjNearbyShopsMapper;
import com.longcai.mapper.nearbyshop.ClyjNearbyShopsSchemeMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.nearbyshop.ClyjNearbyShopsSchemeService;
import com.longcai.util.LoginUser;
import com.longcai.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 附近商铺外卖方案 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-25
 */
@Service
public class ClyjNearbyShopsSchemeServiceImpl extends ServiceImpl<ClyjNearbyShopsSchemeMapper, ClyjNearbyShopsScheme> implements ClyjNearbyShopsSchemeService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjNearbyShopsMapper clyjNearbyShopsMapper;
    @Autowired
    private ClyjNearbyShopsSchemeMapper clyjNearbyShopsSchemeMapper;

    /**
     * 外卖方案列表
     *
     * @param clyjNearbyShopsScheme
     * @param pageHelper
     * @return
     */
    @Override
    public PageTableResponse schemeLists(ClyjNearbyShopsScheme clyjNearbyShopsScheme, PageHelper pageHelper) {

        try {
            //查询登录用户角色
            LoginUser loginUser = UserUtil.getLoginUser();
            //查询店铺id
            QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mich_id", loginUser.getUserId());
            ClyjNearbyShops clyjNearbyShops = this.clyjNearbyShopsMapper.selectOne(queryWrapper);
            //分页查询店铺方案
            Page<ClyjNearbyShopsScheme> page = new Page<ClyjNearbyShopsScheme>(pageHelper.getStart(), pageHelper.getLength());
            QueryWrapper<ClyjNearbyShopsScheme> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("shops_id", clyjNearbyShops.getShopsId());
            queryWrapper1.eq("flag", "0");
            IPage<ClyjNearbyShopsScheme> shopsSchemeIPage = this.clyjNearbyShopsSchemeMapper.selectPage(page, queryWrapper1);
            PageTableResponse pageTableResponse = new PageTableResponse(shopsSchemeIPage.getTotal(),
                    shopsSchemeIPage.getTotal(), shopsSchemeIPage.getRecords());
            logger.debug("查询成功");
            return pageTableResponse;
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id获取外卖方案
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getSchemeInfo(String id) {

        try {
            ClyjNearbyShopsScheme clyjNearbyShopsScheme = this.clyjNearbyShopsSchemeMapper.selectById(id);
            logger.debug("查询成功++++++++++++++", clyjNearbyShopsScheme);
            return ApiResult.success(clyjNearbyShopsScheme);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    /**
     * 新增外卖方案
     *
     * @param clyjNearbyShopsScheme
     * @return
     */
    @Override
    public ApiResult saveScheme(ClyjNearbyShopsScheme clyjNearbyShopsScheme) {
        try {
            ClyjNearbyShopsScheme clyjNearbyShopsScheme1 = clyjNearbyShopsScheme;
            //查询登录用户角色
            LoginUser loginUser = UserUtil.getLoginUser();
            //查询店铺id
            QueryWrapper<ClyjNearbyShops> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mich_id", loginUser.getUserId());
            ClyjNearbyShops clyjNearbyShops = this.clyjNearbyShopsMapper.selectOne(queryWrapper);
            if (clyjNearbyShopsScheme.getStatus().equals("0")) {
                clyjNearbyShopsScheme1.setStatus(null);
            }
            //判断新增启用状态时把其他方案设置为禁用
            if (clyjNearbyShopsScheme.getStatus().equals("1")) {
                QueryWrapper<ClyjNearbyShopsScheme> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("status", "1");
                ClyjNearbyShopsScheme clyjNearbyShopsScheme2 = this.clyjNearbyShopsSchemeMapper.selectOne(queryWrapper1);
                clyjNearbyShopsScheme2.setStatus("2");
                this.clyjNearbyShopsSchemeMapper.updateById(clyjNearbyShopsScheme2);
            }
            clyjNearbyShopsScheme1.setShopsId(clyjNearbyShops.getShopsId());
            clyjNearbyShopsScheme1.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjNearbyShopsScheme1.setCreateTime(new Date());
            clyjNearbyShopsScheme1.setFlag("0");
            Integer integer = this.clyjNearbyShopsSchemeMapper.insert(clyjNearbyShopsScheme1);
            logger.debug("新增成功++++++++++++", integer);
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.error("新增失败");
            e.printStackTrace();
            return ApiResult.fail("新增失败" + e.getMessage());
        }
    }

    /**
     * 修改外卖方案
     *
     * @param clyjNearbyShopsScheme
     * @return
     */
    @Override
    public ApiResult updateScheme(ClyjNearbyShopsScheme clyjNearbyShopsScheme) {
        try {
            clyjNearbyShopsScheme.setUpdateTime(new Date());
            if (clyjNearbyShopsScheme.getStatus().equals("0")) {
                clyjNearbyShopsScheme.setStatus(null);
            }
            //判断新增启用状态时把其他方案设置为禁用
            if (clyjNearbyShopsScheme.getStatus().equals("1")) {
                QueryWrapper<ClyjNearbyShopsScheme> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("status", "1");
                ClyjNearbyShopsScheme clyjNearbyShopsScheme2 = this.clyjNearbyShopsSchemeMapper.selectOne(queryWrapper1);
                clyjNearbyShopsScheme2.setStatus("2");
                this.clyjNearbyShopsSchemeMapper.updateById(clyjNearbyShopsScheme2);
            }
            Integer integer = this.clyjNearbyShopsSchemeMapper.updateById(clyjNearbyShopsScheme);
            logger.debug("修改成功");
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.error("修改失败");
            e.printStackTrace();
            return ApiResult.fail("修改失败" + e.getMessage());
        }
    }


    /**
     * 删除商品类型
     *
     * @param id
     */
    @Override
    public ApiResult deleteById(String id) {
        try {
            QueryWrapper<ClyjNearbyShopsScheme> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            //根据id查询数据
            ClyjNearbyShopsScheme clyjNearbyShopsScheme = this.clyjNearbyShopsSchemeMapper.selectOne(queryWrapper);
            clyjNearbyShopsScheme.setFlag("99");
            clyjNearbyShopsScheme.setUpdateTime(new Date());
            Integer integer = this.clyjNearbyShopsSchemeMapper.updateById(clyjNearbyShopsScheme);
            logger.debug("删除成功");
            return ApiResult.success(integer);
        } catch (Exception e) {
            logger.error("删除失败");
            e.printStackTrace();
            return ApiResult.fail("删除失败" + e.getMessage());
        }
    }
}
